package com.yyk.yykywd.ui.work;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkGive_up_businessInfo;
import com.yyk.yykywd.listenter.DialogSureCancelListenter;
import com.yyk.yykywd.ui.MainActivity;
import com.yyk.yykywd.util.DialogUtil;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.util.ToastUtil;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 原因描述   --合作失败
 */
public class ReasonMiaoshuActivity extends BaseActivity {


    @BindView(R.id.et_defeated)
    EditText etDefeated;
    @BindView(R.id.bt_save)
    Button btSave;
    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_reason_miaoshu;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("原因描述");
    }

    @Override
    public void initData() {
        initGetIntent();

    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
        }
    }

    @OnClick(R.id.bt_save)
    public void onViewClicked() {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }

        if (TextUtils.isEmpty(etDefeated.getText())) {
            ToastUtil.showShort("请填写放弃原因");
            return;
        }

        DialogUtil.showReturnTips(mActivity, "", "原因提交后，此项业务将暂时终止，您可在业务统计中，查看此次业务"
                , "确定", "取消", new DialogSureCancelListenter() {

                    @Override
                    public void onSureListenter() {
                        /**
                         * 接口14：放弃客户whm）
                         */
                        Map<String, String> map = new TreeMap<>();
                        map.put("uid", Hawk.get("uid")+"");
                        map.put("bid", id);
                        map.put("defeated", etDefeated.getText().toString().trim());

                        map.put("sign", Md5Util2.createSign(map));
                        ApiService.getInstance().api.postWorkGive_up_inside(map)
                                .subscribeOn(Schedulers.io())     //调度线程
                                .observeOn(AndroidSchedulers.mainThread())
                                .unsubscribeOn(Schedulers.io())   //取消订阅
                                .subscribe(new Observer<WorkGive_up_businessInfo>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(WorkGive_up_businessInfo info) {
                                        Logger.e("放弃客户" + info.toString());

                                        if (info.getCode() == 1) {
                                            //可以不用 finshi当前页面
                                            startAct(MainActivity.class);

                                        } else {
                                            ToastUtil.showShort(info.getMsg());
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });

                    }

                    @Override
                    public void onCancelListenter() {

                    }
                }, false);


    }
}
